<?php
define( "CCDS_MODE", "BACKEND" );

class ccdsBackend
{
    public $config;
    public $ccdSQL;
    public $smarty;

    public $xajax;

    function __construct( )
    {
        require_once( "base/ccdsBackendPlugin.inc.php" );
        require_once( "base/ccdsBackendUserPlugin.inc.php" );
        require_once( "base/ccdsConfigParser.inc.php" );
        require_once( "base/ccdsConfig.inc.php" );
        require_once( "base/ccdSQL.inc.php" );
        require_once( "base/ccdsXajax.inc.php" );

        require_once( "base/ccdsSmarty.inc.php" );
        require_once( "base/ccdsSmartyFunctions.inc.php" );
        require_once( "base/ccdsModuleManager.inc.php" );
        require_once( "base/ccdsTools.inc.php" );
        require_once( "base/ccdsImageCache.inc.php" );

        require_once( "base/ccdsObjectConfig.inc.php" );
        require_once( "base/ccdsObject.inc.php" );
        require_once( "base/ccdsObjectHandler.inc.php" );
        require_once( "base/ccdsForms.inc.php" );
        require_once( "base/ccdsTableCreator.inc.php" );
        require_once( "base/ccdsVarCache.inc.php" );

        require_once( "base/ccdsBackendUserPlugin.inc.php" );


        //		require_once("base/ObjectHandler.php");

        $this->ObjectConfig=new ccdsObjectConfig();

				$this->objModuleManager=ccdsModuleManager::getInstance();
				$this->objModuleManager->scan();



				
        $smarty=ccdsSmarty::getInstance();

        $smartyFunctions=new ccdsSmartyFunctions();

        $tools=ccdsTools::getInstance();
        $smarty->register_modifier( "htmlentitiesutf8", array
            (
            $tools,
            "smarty_htmlentitiesutf8"
            ));

        $iCache=ccdsImageCache::getInstance();

        $smarty->force_compile=false;
        $smarty->register_function( 'return_resize_img', array
            (
            $iCache,
            'img_cache_return'
            ));

        $smarty->register_function( 'resize_img', array
            (
            $iCache,
            'img_cache_show'
            ));

        $smarty->register_function( 'img_cache_name', array
            (
            $iCache,
            'return_img_name'
            ));

        $this->config=ccdsConfig::getInstance();
        $ccdSQL=ccdSQL::getInstance();
        $ccdSQL->bMasterOnly = true;

    		if ('mysql' == $this->config->get('SESSION_HANDLER') ) 
    		{

    			require('base/ccdsSession.inc.php');
	    		$session = new ccdsSession(); 
					$foo = session_set_save_handler(array($session,"open"),
	                         array($session,"close"),
	                         array($session,"read"),
	                         array($session,"write"),
	                         array($session,"destroy"),
	                         array($session,"gc"));
	      	if (!$foo) die();
	      }
	      

				if (isset($_POST['PHPSESSID'])) {
					session_id($_POST['PHPSESSID']);
				}	      
	      session_start();



        $query="SELECT * FROM languages WHERE lang_code = '" . $this->config->get( 'DEFAULT_LANG' ) . "'";
        $res=$ccdSQL->query( $query );
        $row=$ccdSQL->fetch_assoc( $res );
        define( "MMCMS_DEFAULT_LANG", $row['lang_id'] );
        define( "MMCMS_DEFAULT_LANG_CODE", $this->config->get( 'DEFAULT_LANG' ) );

        $this->config->set( 'MMCMS_DEFAULT_LANG', $row['lang_id'] );

        $lib_dir=$this->config->get( 'LIBRARY_DIR' );
				if (0 < $_SESSION['CMS']['user_interface_lang_id']) {
					$query = "SELECT * FROM languages WHERE lang_id='".$_SESSION['CMS']['user_interface_lang_id']."'";
					$res = $ccdSQL->query($query);
					$row = $ccdSQL->fetch_assoc($res);
					$lang_code = $row['lang_code'];
				} else {
					$lang_code=$this->config->get('DEFAULT_LANG');
				}
        $smarty->config_load( $this->config->get( "DOC_ROOT" ) . $this->config->get( "WEB_ROOT" )
            . $this->config->get( "CMS_ROOT" ) . 'lang/tpl_lang/'.$lang_code.'.conf' );

        require( $lib_dir . "geshi/geshi.php" );
    }

    function loginstatus( &$callnext )
    {

         
        if ('frontend.php' == __FILE__) {
        	$pluginname = 'ccdsFrontendEdit2';
        } else {
        	$pluginname = 'ccdsDefaultPlugin';
        }
        
        $plugin=$this->objModuleManager->get( 'ccdsDefaultPlugin' );

        if ( !$plugin->check_login() && $_REQUEST['ccdsfun'] != 'loginUser' )
        {
            $callnext=array(false);
            $objResponse=new ccdsResponse();
            $objResponse->loadCommands( $plugin->logout() );
            return $objResponse;
        }
    }

    function run( )
    {
        $xajax=ccdsXajax::getInstance();
        $xajax->register( XAJAX_PROCESSING_EVENT, XAJAX_PROCESSING_EVENT_BEFORE, array
            (
            $this,
            "loginstatus"
            ));

        $xajax->processRequest();
//		$xajax->autoCompressJavaScript( $this->config->get( 'LIBRARY_DIR' )."/xajax/xajax_js/xajax_debug.js", true );
//		$xajax->autoCompressJavaScript( $this->config->get( 'DOC_ROOT' ).$this->config->get( 'CMS_WEB_DIR' )."js/backend.js", true );
//		$xajax->autoCompressJavaScript( $this->config->get( 'DOC_ROOT' ).$this->config->get( 'CMS_WEB_DIR' )."js/comet.js", true );
//		$xajax->autoCompressJavaScript( $this->config->get( 'LIBRARY_DIR' )."/xajax/xajax_plugins/response/TabManager/TabManager.js", true );
//		$xajax->autoCompressJavaScript( $this->config->get( 'LIBRARY_DIR' )."/xajax/xajax_plugins/request/swfupload/swfupload.xajax.js", true );
//		$xajax->autoCompressJavaScript( $this->config->get( 'LIBRARY_DIR' )."/xajax/xajax_plugins/request/swfupload/swfupload.js", true );

        
        $plugin=$this->objModuleManager->get( 'ccdsDefaultPlugin' );

        $plugin->run();
    }
}
?>